"""
Created on 13 mrt. 2012

@author: Daan
"""

from pCommon import PortList
from pCommon import RPCHost
from pCommon import Configuration
from pCommon import Defaults

from pCentralServer import CentralServer

import time
import os

class Program:
    """
    """

    configStatic = None

    def __init__( self ):
        """
        """

        defaultConfig = {"centralserver" : { "updateinterval" : Defaults.csUpdateInterval,
                                             "storelocation"  : os.path.join( os.path.dirname( __file__ ), "store" )}}
        Program.configStatic = Configuration.Configuration( "centralserver.ini", defaultConfig )

        self.__updateInterval = float(Program.configStatic.centralserver.updateinterval)

        self.xmlrpc = RPCHost.xmlrpcserver( "localhost", PortList.CENTRALSERVER, self.shutdown )
        self.cont = True

        self.cs = CentralServer.CentralServerFactory( Program.configStatic.centralserver.storelocation )

        self.xmlrpc.register_instance( self.cs )

    def shutdown( self ):
        """
        """

        self.cont = False

        return True

    def run( self ):
        """
        """

        while self.cont:
            self.xmlrpc.handle_request()
            self.cs._handleIntervalMethods() # pylint: disable-msg=W0212

            time.sleep( self.__updateInterval )

def main():
    Program().run()

if __name__ == '__main__':
    main()
